package controllers

import (
	"github.com/astaxie/beego"
	"shuxianghui/models"
	"HTshuxianghui/htmodels"
	"strings"
	"time"
)

type MainController struct {
	beego.Controller
}

func (c *MainController) Get() {

	level := c.GetSession("utype")
	if level == nil {
		Message(&c.Controller, `/login`, "登录信息错误", "", false)
	}
	c.Data["utype"] = level.(int)

	c.Data["name"] = c.GetSession("name").(string)
	typeName := ""
	usertype := c.GetSession("utype").(int)
	if usertype == 1 {
		typeName = "VIP用户"
	} else if usertype == 2 {
		typeName = "发布员"
	} else if usertype == 10 {
		typeName = "会长"
	} else if usertype == 100 {
		typeName = "分权管理员"
	} else if usertype == 1000 {
		typeName = "管理员"
	}
	c.Data["typeName"] = typeName

	c.TplName = "index.html"
}

func (c *MainController) Index_v1() {
	uid := c.GetSession("uid").(int)
	utype := c.GetSession("utype").(int)
	cityid := c.GetSession("city").(int)
	c.Data["utype"] = utype

	var cityList []htmodels.CityId_int
	if c.GetSession("utype").(int) == 100 {
		cityList = getPermissionCityList(c.GetSession("uid").(int))
	}

	if utype > 1 {
		totalnum := int64(0)
		vipnum := int64(0)
		registnum := int64(0)
		if c.GetSession("utype").(int) == 100 && len(cityList) == 0 {

		} else {
			totalnum, _ = htmodels.GetLocalUserCount(cityid, -1, cityList)
			vipnum, _ = htmodels.GetLocalUserCount(cityid, 1, cityList)
			registnum, _ = htmodels.GetLocalUserCount(cityid, 0, cityList)
		}

		c.Data["totalnum"] = totalnum
		c.Data["vipnum"] = vipnum
		c.Data["registnum"] = registnum
	}

	quanbu := int64(0)
	weishenhe := int64(0)
	bohui := int64(0)
	chushen := int64(0)
	fabu := int64(0)

	if utype > 1 {
		if c.GetSession("utype").(int) == 100 && len(cityList) == 0 {

		} else {
			quanbu, _ = htmodels.HTArticleGetMyArticleInfo(0, cityid, -1, cityList)
			weishenhe, _ = htmodels.HTArticleGetMyArticleInfo(0, cityid, 0, cityList)
			bohui, _ = htmodels.HTArticleGetMyArticleInfo(0, cityid, 1, cityList)
			chushen, _ = htmodels.HTArticleGetMyArticleInfo(0, cityid, 2, cityList)
			fabu, _ = htmodels.HTArticleGetMyArticleInfo(0, cityid, 3, cityList)
		}

	} else {
		quanbu, _ = htmodels.HTArticleGetMyArticleInfo(uid, cityid, -1)
		weishenhe, _ = htmodels.HTArticleGetMyArticleInfo(uid, cityid, 0)
		bohui, _ = htmodels.HTArticleGetMyArticleInfo(uid, cityid, 1)
		chushen, _ = htmodels.HTArticleGetMyArticleInfo(uid, cityid, 2)
		fabu, _ = htmodels.HTArticleGetMyArticleInfo(uid, cityid, 3)
	}
	c.Data["quanbu"] = quanbu
	c.Data["weishenhe"] = weishenhe
	c.Data["bohui"] = bohui
	c.Data["chushen"] = chushen
	c.Data["fabu"] = fabu

	c.TplName = "index_v1.html"
}

func (c *MainController)Login() {
	if uid := c.GetSession("uid"); uid != nil {
		c.Redirect("/", 302)
	}
	username := c.GetString("user")
	pass := c.GetString("pass")

	if Login_ManyPeople(c) {
		htmodels.InsertLog(&models.SQLLogs{UserId:c.GetSession("uid").(int), UserName:c.GetSession("name").(string), Body:"login", LogTime:time.Now(), LogType:htmodels.LogType_Login, Status:0})
		c.Redirect("/", 302)
	}

	if username != "" {
		user := models.Users{Phone:username}
		emailOrPhone := false
		if strings.Index(username, "@") != -1 {
			emailOrPhone = true
		}
		if htmodels.HTCheckUserLogin(&user, username, GetPassword(pass), emailOrPhone) {
			if user.UserType < 1 {
				c.Data["wronginfo"] = true
			}
			c.SetSession("uid", user.Id)
			c.SetSession("utype", user.UserType)
			c.SetSession("city", user.CityId)
			c.SetSession("name", user.Username)
			c.Redirect("/", 302)
		} else {
			c.Data["wronginfo"] = true
		}
	}
	c.TplName = "login.html"
}

func (c *MainController)Logout() {
	c.DelSession("uid")
	c.DelSession("utype")
	c.DelSession("city")
	c.DelSession("name")
	c.Redirect("/login", 302)
}

func Auth_Indexhtml(table string, userlever int) bool {
	if (table == "Dushuhui" || table == "Zhuli") {
		if userlever == 10 {
			return true
		} else {
			return false
		}
	}

	if (table == "Xiaoxi") {
		if userlever >= 100 {
			return false
		} else {
			return true
		}
	}
	if userlever == 1000 {
		return true
	}

	if table == "City" || table == "Comment" || table == "Goods" || table == "Messages" || table == "Order" {
		if userlever != 1000 {
			return false
		} else {
			return true
		}
	}
	if table == "Users" {
		if userlever == 1000 {
			return true
		} else {
			return false
		}
	}
	if table == "Search" {
		if userlever >= 100 {
			return true
		} else {
			return false
		}
	}
	if table == "Article" {
		return true
	}

	return false
}

func Auth_ArticleType(ArticleType int, userlevel int) bool {
	if userlevel >= 100 {
		return true
	}
	if (!inPermissionA(ArticleType)) && (!inPermissionB(ArticleType)) {
		if userlevel >= 100 {
			return true
		} else {
			return false
		}
	}
	if userlevel >= 2 {
		return true
	}
	if inPermissionB(ArticleType) {
		if userlevel == 1 {
			return true
		}
	}
	return false
}

func Login_ManyPeople(c *MainController) bool {
	username := c.GetString("user")
	pass := c.GetString("pass")
	if username == "admin@admin.com" {
		if pass == "admin456" {
			c.SetSession("uid", 0)
			c.SetSession("utype", 1000)
			c.SetSession("city", 0)
			c.SetSession("name", "超级管理员")
			return true
		}
	}
	if username == "18600813096" {
		if pass == "admin456" {
			c.SetSession("uid", 0)
			c.SetSession("utype", 1000)
			c.SetSession("city", 0)
			c.SetSession("name", "粟博莉")
			return true
		}
	}
	if username == "18210150365" {
		if pass == "admin456" {
			c.SetSession("uid", 0)
			c.SetSession("utype", 1000)
			c.SetSession("city", 0)
			c.SetSession("name", "杨 乐")
			return true
		}
	}
	//phone := []string{
	//	"13522375127", "18501368039", "13311237130", "13488804123", "13311314159", "13651186839", "18311012773", "15901270745", "13661321785", "18001397110", "15116941772", "15810129478", "13974801353", "13522601918", "13911363009",
	//}
	//password := []string{
	//	"admin123", "admin123", "admin123", "admin123", "admin123", "admin123", "admin123", "admin123", "admin123", "admin123", "admin123", "admin123", "admin123", "admin123", "admin123",
	//}
	//name := []string{
	//	"董  亮", "夏  雨", "崔欣欣", "王  缘", "王  梅", "勾敬芳", "常  鸿", "杨  盼", "张永华", "黄博洋", "高  素", "范阳阳", "李  艳", "张玉华", "李老师",
	//}
	//for i, _ := range phone {
	//	if phone[i] == username {
	//		if pass == password[i] {
	//			c.SetSession("uid", 0)
	//			c.SetSession("utype", 1000)
	//			c.SetSession("city", 0)
	//			c.SetSession("name", name[i])
	//			return true
	//		}
	//	}
	//}

	return false
}

//type
//0 活动咨询
//1 作品选登
//2 天使风采
//3 读书会简介
//4 读书会活动
//5 书香讲堂
//6 天下书坊
//7 家生活
//8 书坊创意设计